<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <div id="app">
    <h2>DOM 更新时机</h2>
    <p>count: {{ count }}</p>
    <button v-on:click="increment">btn</button>
  </div>

  <script src="./lib/vue3.js"></script>
  <script>
    const { createApp } = Vue;

    // 数据的改变和页面渲染不是一起完成的，异步的
    // 为了尽可能地减少dom的重新渲染的次数

    // this.$nextTick 里面的回调函数就是等待页面渲染完成之后再调用

    createApp({
      data () {
        return {
          count: 1,
          msg: "hi",
        };
      },
      methods: {
        increment () {
          // 直接从1变成了4
          // this.count++;
          // this.count++;
          // this.count++;
          // this.msg = "hello";
          // console.log(this.count);
          // console.log(document.querySelector("p").innerHTML);

          this.count++;
          // setTimeout(() => {
          //   this.count++;
          // }, 0);
          this.$nextTick(() => {
            this.count++;
          });
        },
      },
    }).mount("#app");
  </script>
</body>

</html>
